package com.app.brilliant.system.service.impl;
import com.app.brilliant.common.service.impl.BaseServiceImpl;
import com.app.brilliant.system.scheduled.bean.ScheduleJobExample;
import com.app.brilliant.system.scheduled.constance.ScheduleConstant;
import com.app.brilliant.system.scheduled.util.ScheduleUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.app.brilliant.system.entity.ScheduleJob;
import com.app.brilliant.system.mapper.ScheduleJobExtandMapper;
import com.app.brilliant.system.mapper.ScheduleJobMapper;
import com.app.brilliant.system.service.ScheduleJobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.CronTrigger;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
public class ScheduleJobServiceImpl extends BaseServiceImpl<ScheduleJobExtandMapper, ScheduleJob> implements ScheduleJobService {
    @Autowired
    private Scheduler scheduler;

    @Autowired
    private ScheduleJobMapper scheduleJobMapper;

    @Autowired
    private ScheduleJobExtandMapper scheduleJobExtandMapper;
    /**
     * 项目启动时，初始化定时器
     */
    @PostConstruct
    public void init(){
        ScheduleJobExample scheduleJobExample = new ScheduleJobExample();

        List<ScheduleJob> scheduleJobList = scheduleJobMapper.selectByExample(scheduleJobExample);

        for(ScheduleJob scheduleJob : scheduleJobList){
            CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, scheduleJob.getJobId());
            //如果不存在，则创建
            if(cronTrigger == null) {
                ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
            }else {
                ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
            }
        }
    }

    @Override
    public PageInfo queryPage(Map<String, Object> params) {
        String beanName = (String)params.get("beanName");
        int page = Integer.parseInt(params.getOrDefault("page", "1").toString());
        int pageSize = Integer.parseInt(params.getOrDefault("pageSize", "10").toString());
        PageHelper.startPage(page,pageSize);
        ScheduleJobExample scheduleJobExample = new ScheduleJobExample();
        scheduleJobExample.createCriteria().andBeanNameLike(beanName);
        List<ScheduleJob> scheduleJobs = scheduleJobMapper.selectByExample(scheduleJobExample);
        PageInfo pageInfo = new PageInfo<>(scheduleJobs);
        return pageInfo;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(ScheduleJob scheduleJob) {
        scheduleJob.setCreateTime(new java.util.Date(System.currentTimeMillis()));
        scheduleJob.setStatus(Byte.parseByte(ScheduleConstant.NORMAL+""));
        scheduleJobMapper.insertSelective(scheduleJob);
        ScheduleUtils.createScheduleJob(scheduler, scheduleJob);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ScheduleJob scheduleJob) {
        ScheduleUtils.updateScheduleJob(scheduler, scheduleJob);
        scheduleJobMapper.updateByPrimaryKeySelective(scheduleJob);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<Long> jobIds) {
        for(Long jobId : jobIds){
            ScheduleUtils.deleteScheduleJob(scheduler, jobId);
        }

        //删除数据
        scheduleJobExtandMapper.deleteBatch(jobIds);
    }

    @Override
    public int updateBatch(List<Long>  jobIds, int status){
        return scheduleJobExtandMapper.updateBatch(jobIds,status);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void run(List<Long> jobIds) {
        for(Long jobId : jobIds){
            ScheduleUtils.run(scheduler, scheduleJobMapper.selectByPrimaryKey(jobId));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pause(List<Long> jobIds) {
        for(Long jobId : jobIds){
            ScheduleUtils.pauseJob(scheduler, jobId);
        }
        if(jobIds.size()>0){
            updateBatch(jobIds, ScheduleConstant.PAUSE);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void resume(List<Long> jobIds) {
        for(Long jobId : jobIds){
            ScheduleUtils.resumeJob(scheduler, jobId);
        }
        if(jobIds.size()>0){
            updateBatch(jobIds, ScheduleConstant.NORMAL);
        }
    }
}
